package com.ruoyi.web.controller.admin.design;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.OSSUtils;
import com.ruoyi.system.service.CaseService;
import com.ruoyi.system.vo.CaseDetailVo;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("admin/case")
public class AdminCaseController {
    @Resource
    private CaseService caseService;
    @Resource
    private OSSUtils ossUtils;
    /**
     * 案例编辑
     */
    @PostMapping("/edit")
    public AjaxResult editCase(@RequestBody CaseDetailVo vo){
        if(ObjectUtils.isEmpty(vo.getId())){
            return AjaxResult.error("主键不能为空");
        }
        return AjaxResult.success(caseService.editCase(vo));
    }
    /**
     * 案例详情
     */
    @PostMapping("/query")
    public AjaxResult queryCase(String id){
        if(ObjectUtils.isEmpty(id)){
            return AjaxResult.error("主键不能为空");
        }
        return AjaxResult.success(caseService.queryCase(id));
    }
    /**
     * 案例模糊搜索
     */
    @PostMapping("/query/condition")
    public AjaxResult querByCondition(@RequestBody CaseDetailVo vo){
        return AjaxResult.success(caseService.queryCaseByPage(vo));
    }
    /**
     * 案例新增
     */
    @PostMapping("/add")
    public AjaxResult addCase(@RequestBody CaseDetailVo vo){
        return AjaxResult.success(caseService.addCase(vo));
    }
    /**
     * 案例逻辑删除
     */
    @PostMapping("delete/case")
    public AjaxResult deleteCaseByid(Long id){
        if(ObjectUtils.isEmpty(id)){
            return AjaxResult.error("主键不能为空");
        }
        return AjaxResult.success(caseService.deleteCaseById(id));
    }
    /**
     * 案例oss上传
     */
    @PostMapping("/upload")
    public AjaxResult uploadOss(@RequestParam("file")MultipartFile file){
        OSSUtils.OssFileInfo fileInfo = ossUtils.upload(file, new String());
        String downloadUrl = fileInfo.getDownloadUrl();
        return AjaxResult.success(downloadUrl);
    }

    /**
     * 通过设计师id差他的案例
     */
    @PostMapping("/query/case/by/designer")
    public AjaxResult queryCaseByDesignerId(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            return AjaxResult.error("设计师id不能为空");
        }
        List<CaseDetailVo> caseDetailVoList = caseService.queryCaseByDesignerId(id);
        return AjaxResult.success(caseDetailVoList);
    }
}
